<?php
App::uses('AppModel', 'Model');
/**
 * ReponsesDonnee Model
 *
 * @property site $site
 * @property questionnaire $questionnaire
 * @property question $question
 * @property article $article
 * @property reponse $reponse
 */
class ReponsesDonnee extends AppModel {

	public function reponseDonnees($idQuestionnaire, $idQuestion) {
		return $this->query("SELECT * FROM reponses_donnees WHERE id_questionnaire=$idQuestionnaire AND id_question=$idQuestion");
	}
	
	public function reponsesDonneesQuestionnaire($id_questionnaire) {
		return $this->query("SELECT * FROM reponses_donnees WHERE id_questionnaire=$id_questionnaire AND id_site IS NULL");
	}
	
	public function reponsesDonneesDuSite($id_site) {
		return $this->query("SELECT * FROM reponses_donnees WHERE id_site=$id_site");
	}
	
	public function insertAvecReponses($id_questionnaire, $id_site, $reponses) {
		$this->query("INSERT INTO reponses_donnees (".
						"reponse_entier, ".
						"reponse_donnee_texte, ".
						"reponse_donnee_oui_non, ".
						"reponse_quantite_article, ".
						"id_questionnaire, ".
						"id_question, ".
						"id_site, ".
						"id_reponse, ".
						"id_article".
					") ".
					"VALUES (".
						((isset($reponses['reponse_entier']) && !empty($reponses['reponse_entier'])) ? $reponses['reponse_entier'] : 'NULL').", ".
						((isset($reponses['reponse_donnee_texte']) && !empty($reponses['reponse_donnee_texte'])) ? "'".$reponses['reponse_donnee_texte']."'" : 'NULL').", ".
						((isset($reponses['reponse_donnee_oui_non']) && !empty($reponses['reponse_donnee_oui_non'])) ? $reponses['reponse_donnee_oui_non'] : 'NULL').", ".
						((isset($reponses['reponse_quantite_article']) && !empty($reponses['reponse_quantite_article'])) ? $reponses['reponse_quantite_article'] : 'NULL').", ".
						$id_questionnaire.", ".
						((isset($reponses['id_question']) && !empty($reponses['id_question'])) ? $reponses['id_question'] : 'NULL').", ".
						$id_site.", ".
						((isset($reponses['id_reponse']) && !empty($reponses['id_reponse'])) ? $reponses['id_reponse'] : 'NULL').", ".
						((isset($reponses['id_article']) && !empty($reponses['id_article'])) ? $reponses['id_article'] : 'NULL').
					")");
	}
	
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_reponse_donnee';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_reponse_donnee' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'reponse_entier' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'reponse_donnee_oui_non' => array(
			'boolean' => array(
				'rule' => array('boolean'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'reponse_quantite_article' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'site' => array(
			'className' => 'site',
			'foreignKey' => 'id_site',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'questionnaire' => array(
			'className' => 'questionnaire',
			'foreignKey' => 'id_questionnaire',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'question' => array(
			'className' => 'question',
			'foreignKey' => 'id_question',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'article' => array(
			'className' => 'article',
			'foreignKey' => 'id_article',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'reponse' => array(
			'className' => 'reponse',
			'foreignKey' => 'id_reponse',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
}
